package com.changgou.canal.listener;


import com.alibaba.otter.canal.protocol.CanalEntry;
import com.changgou.canal.config.RabbitMQConfig;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@CanalEventListener // 声明当前类为canal监听类
public class BusinessListener {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * canal监听数据库更新数据
     * @param eventType 操作的数据库类型
     * @param rowData  操作的数据
     */
    @ListenPoint(schema = "changgou_business",table = "tb_ad") //监听的数据库和表
    public void adUpdate(CanalEntry.EventType eventType,CanalEntry.RowData rowData){
        System.out.println("data changed");
        //rowData.getBeforeColumnsList().forEach((c) -> System.out.println("改变前的数据:"+c.getName()+"--"+c.getValue()));

        //rowData.getAfterColumnsList().forEach((c) -> System.out.println("改变后的数据:"+c.getName()+"--"+c.getValue()));

        String positionOri = null;

        Boolean result = false;
        for (CanalEntry.Column column : rowData.getAfterColumnsList()) {
            if("position".equals(column.getName())){
               /* if(positionOri == null){
                    positionOri = column.getValue();
                }else {
                    result = (positionOri.equals(column.getValue())?false:true);
                }*/
                System.out.println("数据改变"+column.getValue());
                rabbitTemplate.convertAndSend("", RabbitMQConfig.AD_UPDATE_QUEUE,column.getValue());
            }
        }


        for (CanalEntry.Column column : rowData.getBeforeColumnsList()) {
            if(/*result && */"position".equals(column.getName())){
                System.out.println("数据改变"+column.getValue());
                rabbitTemplate.convertAndSend("", RabbitMQConfig.AD_UPDATE_QUEUE,column.getValue());
            }
        }



    }
}
